package com.ggh.jinjilive.ui.mall.order.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemOrderBinding;
import com.ggh.jinjilive.databinding.ItemShopPingCarAffirmBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mall.goods.GoodsShopSPMobile;
import com.ggh.jinjilive.mobile.mall.order.MineOrder;
import com.ggh.jinjilive.mobile.mall.shopping.ShopMessageMoblie;
import com.ggh.jinjilive.ui.mall.goods.affirm.adaoter.ShopingCarAffirmGoodsAdapter;

import java.util.List;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ItemViewHolder> {


    private List<MineOrder> mineOrderList;

    public void setAdd(List<MineOrder> mineOrderList) {
        this.mineOrderList = mineOrderList;
        notifyDataSetChanged();
    }


    public void Add(List<MineOrder> mineOrderList) {
        if (null != this.mineOrderList) {
            this.mineOrderList.addAll(mineOrderList);
        } else {
            this.mineOrderList = mineOrderList;
        }
        notifyDataSetChanged();
    }

    private AddItemOnClickListener addItemOnClickListener;

    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }


    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemOrderBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_order, parent, false);
        return new ItemViewHolder(binding);
    }


    public void onBindViewHolder(@NonNull ItemViewHolder holder, final int position) {
        MineOrder mineOrder = mineOrderList.get(position);
        if (null != mineOrder) {
            //店铺图片
            Glide.with(holder.binding.storeImg).load(mineOrder.getImg()).into(holder.binding.storeImg);
            //店铺名称
            holder.binding.storeName.setText(mineOrder.getName());
            //未退款或退款失败
            if (mineOrder.getIs_refund() != 1){
                //状态
                switch (mineOrder.getStatus()){
                    case 0://待付款
                        holder.binding.tvStatus.setText("待付款");
                        holder.binding.buttonThree.setText("付款");
                        holder.binding.buttonThree.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if(null != addItemOnClickListener){
                                    addItemOnClickListener.onSkipClicked(99999,mineOrder);
                                }
                            }
                        });
                        break;
                    case 1://待发货
                        holder.binding.tvStatus.setText("待发货");
                        holder.binding.buttonThree.setText("提醒发货");
                        holder.binding.buttonTwo.setText("申请退款");
                        holder.binding.buttonTwo.setVisibility(View.VISIBLE);
                        break;
                    case 2://待收货
                        holder.binding.tvStatus.setText("待收货");
                        holder.binding.buttonThree.setText("确认收货");
                        holder.binding.buttonTwo.setText("查看物流");
                        holder.binding.buttonTwo.setVisibility(View.VISIBLE);
                        holder.binding.buttonOne.setText("申请退款");
                        holder.binding.buttonOne.setVisibility(View.VISIBLE);
                        break;
                    case 3://已完成
                        holder.binding.tvStatus.setText("已完成");
                        holder.binding.buttonThree.setText("删除");
                        holder.binding.buttonTwo.setText("申请退款");
                        holder.binding.buttonTwo.setVisibility(View.VISIBLE);
                        break;
                    case 4://已退款
                        holder.binding.tvStatus.setText("已退款");
                        holder.binding.buttonThree.setText("删除");
                        break;
                }
            }else {
                holder.binding.tvStatus.setText("退款中");
                holder.binding.buttonThree.setText("退款进度");
            }
            if(null != mineOrder.getProduct() && mineOrder.getProduct().size() > 0){
                OrderGoodsAdapter orderGoodsAdapter = new OrderGoodsAdapter();
                orderGoodsAdapter.setAdd(mineOrder.getProduct());
                holder.binding.rvGoods.setLayoutManager(new LinearLayoutManager(holder.binding.rvGoods.getContext()));
                holder.binding.rvGoods.setAdapter(orderGoodsAdapter);
            }

        }
    }


    @Override
    public int getItemCount() {

        return null == mineOrderList ? 0 : mineOrderList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemOrderBinding binding;

        public ItemViewHolder(@NonNull ItemOrderBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}